package com.example.jussi.dynamicbroadcastdemo;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    MyBroadCast1 receiver;
    MyBroadCast2 receiver2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 和静态广播唯一的区别
         * 静态在 manifest 里注册
         * 动态在 onCreate 里用代码注册
         *
         * 注册了一定要记得在 onDestroy 里解注册
         * 故动态广播的生存期为当前活动的生存期
         */
        receiver = new MyBroadCast1();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.jussi1");
        registerReceiver(receiver, filter);

        receiver2 = new MyBroadCast2();
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(receiver2, filter2);
    }

    public void send(View hhView){
        Intent intent = new Intent();
        intent.setAction("com.jussi1");
        sendBroadcast(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解注册
        unregisterReceiver(receiver);
        unregisterReceiver(receiver2);
    }
}
